﻿using System;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using Autofac;
using Autofac.Configuration;
using Autofac.Extras.DynamicProxy2;
using Autofac.Integration.Mvc;
using Castle.DynamicProxy;
using OrchardFork.Framework.Aop;
using OrchardFork.Framework.Data;
using OrchardFork.Framework.Environment.AutofacUtil;
using OrchardFork.Framework.Events;
using OrchardFork.Framework.Mvc.Routes;

namespace OrchardFork.Framework.Environment
{
    public static class CnvpStarter
    {
        public static IContainer CreateHostContainer(Action<ContainerBuilder> registrations)
        {
            var builder = new ContainerBuilder();

            //注册组件，autofac解析接口集合
            builder.RegisterModule(new CollectionOrderModule());

            //全局AOP拦截器注入
            builder.RegisterType<InterceptorService>().Named<IInterceptor>("AOP").InstancePerLifetimeScope();

            //事务接口注入
            builder.RegisterType<TransactionAttribute>().As<ITransactionAttribute>().InstancePerLifetimeScope();

            var typeList = TypeUtil.FilterTypesInAssemblies(type => true, assembly => true).ToList();

            //路由发布
            var routesProviderTypeList = typeList.Where(type => typeof(IRouteProvider).IsAssignableFrom(type)).ToArray();
            builder.RegisterTypes(routesProviderTypeList).AsImplementedInterfaces().SingleInstance();

            //控制器发布
            var controllerProviderTypeList = typeList.Where(type => typeof(IController).IsAssignableFrom(type)).ToArray();
            builder.RegisterTypes(controllerProviderTypeList);

            //继承IEventHandler的所有接口注入
            var serviceProviderTypeList = typeList.Where(type => typeof(IEventHandler).IsAssignableFrom(type)).ToArray();
            //foreach (var type in serviceProviderTypeList)
            //{
            //    builder.RegisterType(type).AsImplementedInterfaces().EnableInterfaceInterceptors().InstancePerHttpRequest();
            //}
            builder.RegisterTypes(serviceProviderTypeList).AsImplementedInterfaces().EnableInterfaceInterceptors().InstancePerLifetimeScope();

            //路由发布器
            builder.RegisterType<RoutePublisher>().As<IRoutePublisher>().SingleInstance();
            //命令执行器
            builder.RegisterType<DefaultCnvpShell>().As<ICnvpShell>().SingleInstance();
            //主站启动器
            builder.RegisterType<DefaultCnvpHost>().As<ICnvpHost>().SingleInstance();

            var autofacSection = ConfigurationManager.GetSection(ConfigurationSettingsReaderConstants.DefaultSectionName);
            if (autofacSection != null)
                builder.RegisterModule(new ConfigurationSettingsReader());

            registrations(builder);

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            DefaultHostContainer.Container = container;
            return container;
        }

        public static ICnvpHost CreateHost(Action<ContainerBuilder> registrations)
        {
            var container = CreateHostContainer(registrations);
            return container.Resolve<ICnvpHost>();
        }
    }
}
